<%@page contentType="text/html; charset=utf-8" %>
<%@ include file="/commons/taglibs.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>提交责任部门</title>
<script language="JavaScript" type="text/javascript" src="${ctx}/scripts/public.js"></script>
<script language="JavaScript" type="text/javascript" src="${ctx}/scripts/public3.js"></script>

<script language="JavaScript" type="text/javascript" src="${ctx}/scripts/ajaxException.js"></script>
<script language="JavaScript" type="text/javascript" src="${ctx}/scripts/common.js"></script>
<script language="JavaScript" type="text/javascript" src="${ctx}/scripts/div/common.js"></script>

<link href="${ctx}/styles/aux_window.css" rel="stylesheet" type="text/css" />
<%@ include file="/commons/validation.jsp"%>
<script type="text/javascript">
	var winObject;

	var principalInfo = new Array(); //二维数组,每个一维数组中第一个是部门ID
	var departNames = new Array();
	var empNames = new Array();

/******************此代码一般人看不懂*****************************/
/****************选择部门*********************/
	function openSelectDep(){//打开选部门的页面
  		var url = "${ctx}/common_employeeQuery.do?method=getAllDeptList&hadSelectDeptId=" + getDepartment();
  		url += "&frameName=taskSubmitFrame";
  		url += "&&callBack=setDepInfo";
		top.windowOpen(url, "selectPerson", "选部门", 530, 700,true,"selectPerson");
	}
	function setDepInfo(info){ //设置部门信息
		var departments = info.split(";");
		var tempDepartmentId = new Array(); //缓存所有选择的部门ID
		var temp = null;
		for(var i = 0 ; i < departments.length; i++){
			temp = departments[i].split(",");//获取选择的部门信息
			addDepartment(temp[0],temp[1]);
			tempDepartmentId.push(temp[0]);
		}
		removeDepartment(tempDepartmentId);//对开始选择的 合现在选择的  删除没被选择的
		showSelected();
	}
	function addDepartment(depId,depName){ //添加部门信息
		var notExist = true; //不存在标示
		for(var i = 0; i < principalInfo.length; i++){
			if(principalInfo[i][0] == depId){
				notExist = false;
				break; //相等则说明存在,直接返回
			}
		}
		if(notExist){ //不存在,则添加部门信息
			principalInfo[principalInfo.length] = new Array(depId);
			departNames[departNames.length] = depName;
		}
	}
	function removeDepartment(selected){//删除没有选择的部门信息

		for(var i = 0; i < principalInfo.length; i++){
			var flag = _checkExist(selected,i);
			if(!flag){ //不存在,灭掉
				_removeEmployNames(principalInfo[i]); //首先灭员工姓名  principalInfo[index] 为将要删除的部门
				_removeEmployIds(principalInfo[i][0]); //其次删除员工ID
				principalInfo.splice(i,1); //然后灭掉将要传入到后台的值
				departNames.splice(i,1); //最后灭掉 部门名称
				i--;
			}
		}
	}
	//删除对应被删除部门的员工ID
	function _removeEmployIds(deleteDepartId){
		for(var i = 0; i < hasSelectedUserId.length; i++){
			//如果部门ID相等 则删除
			if(hasSelectedUserId[i][1] == deleteDepartId){
				hasSelectedUserId.splice(i,1);
				i--;
			}
		}
	}
	function _removeEmployNames(selectInfo){ //删除员工姓名 selectInfo是已经删除的部门
		for(var i = 0; i < empNames.length; i++){
			for(var j = 0; j < selectInfo.length; j++){
				if(empNames[i] == selectInfo[j]){
					empNames.splice(i,1);
					i--;
				}
			}
		}
	}

	function _checkExist(selected,index){ //判断是否存在了
			var isExist = false; //是否存在标示 true 表示存在, false  表示不存在

			for(var i = 0; i < selected.length; i++){
				if(selected[i] == principalInfo[index][0]){
					isExist = true;
					break;
				}
			}
			return isExist;
	}
	function getDepartment(){ //获取所有已选择的部门ID
		var depSelected = new Array();
		for(var i = 0; i < principalInfo.length; i++){
			depSelected.push(principalInfo[i][0]);
		}
		return depSelected.toString();
	}
/***************选择人*****************/
	var hasSelectedUserId = new Array(); //缓存已选择的客户ID  选择人的页面使用

	function openSelectPerson(){ //打开选人的界面
	  	var url = "${ctx}/common_employeeQuery.do?method=initSelectPsrsonMain";
	  	url += "&frameName=taskSubmitFrame";
	  	url += "&callBack=setPersonInfo";
	  	url += "&hadSelectUserId=" +getEmployeeIds();
	  	url += "&multiple=true";
	  	url += "&allowNull=true"
		top.windowOpen(url, "selectPerson", "选人", 530, 700,true,"selectPerson");
	}
	function setPersonInfo(info){ //设置选人信息
		var personInfos = info.split(";");
		var temp = null;
		hasSelectedUserId = new Array();
		empNames = new Array();
		clearEmployee();
		for(var i = 0; i < personInfos.length; i++){
			temp = personInfos[i].split(",");
			addEmployee(temp[2],temp[0],temp[3],temp[1]);
		}
		showSelected();
	}
	function addEmployee(depId,empId,depName,empName){ //添加员工
		var tempEmployee = new Array(empId,depId);
		hasSelectedUserId.push(tempEmployee);//缓存已选择的客户

		for(var i = 0; i < principalInfo.length; i++){
			if(principalInfo[i][0] == depId){ //如果该部门已经存在于数组中 则添加员工
				principalInfo[i][principalInfo[i].length] = empName; //该员工不存在添加
				empNames[empNames.length] = empName;
				return;
			}
		}
		//经过上面的判断,说明部门不存在,则新添加一个
		principalInfo[principalInfo.length]  = new Array(depId,empName);
		empNames[empNames.length] = empName;
		departNames[departNames.length] = depName;
	}
	function getEmployeeIds(){
		var ids = new Array();
		for(var i = 0; i < hasSelectedUserId.length; i++){
			ids.push(hasSelectedUserId[i][0]);
		}
		return ids.toString();
	}
	function clearEmployee(){
		for(var i = 0; i < principalInfo.length; i++)
			principalInfo[i].splice(1); //删除对应部门全部员工
	}

	/*********选人选部门的公用方法--显示所选择的部门何人*************/
	function showSelected(){
		$("dep").value = departNames.toString();
		$("deptPrincipalName").value = empNames.toString();
		//设置选择的值
		var principal = new Array();
		for(var i = 0; i < principalInfo.length; i++){
			principal[principal.length] = principalInfo[i].join("-");
		}

		$("principals").value = principal.toString();
	}
/**************end***************/
	function validationDeption(){
		var url = "${ctx}/customerService_deptCooperationAction.do?method=";
		var param = "principals=" + $("principals").value;
			param += "&custServiceEventId=" + $("custServiceEventId").value;
		winObject.ajaxSubmit(url,param,function(result){
			findFrame("main").refleshFrame();
			windowClose();
		});
	}
	function submitForm(){ //提交填写的信息
		var flag = $("taskSubmitForm").fireEvent("onsubmit");
		if(!flag) return;

		var url = "${ctx}/customerService_deptCooperationAction.do?method=addTask"
		var param = "principals=" + $("principals").value;
			param += "&custServiceEventId=" + $("custServiceEventId").value;
		winObject.ajaxSubmit(url,param,function(result){
			ajaxSubmitException(result.responseText);
			findFrame("main").refleshFrame();
			windowClose();
		});
	}
/***************************/
	function init(){
		var buttons = new Array("btn1_12","btn1_32");
		winObject = new WindowObject("taskSubmitForm",buttons);
	}
</script>
</head>

<body onload="init();">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td colspan="2" class="window_head_outBg">
	    <table border="0" cellpadding="0" cellspacing="0">
	      <tr>
	        <td>
	        	<span class="window_head_intext">
	        	<img src="${ctx}/images/ico_Advance.gif" align="absmiddle" /> 提交责任部门</span>
	        </td>
	      </tr>
	    </table>
    </td>
  </tr>
  <tr>
    <td colspan="2" class="aux_header_line_1"></td>
  </tr>
  <tr>
    <td colspan="2" class="aux_header_line_2"></td>
  </tr>
  <tr>
  	 <form id="taskSubmitForm" method="post" action="" class="required-validate">
    <td colspan="2" class="content">

    <input type="hidden" id="custServiceEventId" name="" value="${keyword}" >
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td class="aux_outtable_color">
        <table width="100%" border="0" cellpadding="0" cellspacing="1" class="aux_intable_color">
          <tr>
            <td valign="top" class="aux_intable_td1">接单部门：</td>
            <td width="28%" class="aux_intable_require">
            	<table border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td>
                	<!-- validate-ajax -->
                	<input id="dep" name="dep" type="text" value="" size="16" readonly="readonly"
                			class="inputtext required  max-length-100 "
                			afterInsert="depErrorImage"/>
                	<input type="hidden" id="principals" name="principals">
                </td>
                <td width="4">&nbsp;</td>
                <td>
                	<a href="#"  onClick="openSelectDep();"onFocus="this.blur()">
                		<img src="${ctx}/images/ico_cz.gif" width="16" height="16" border="0" />
                	</a>
                	<span id="depErrorImage"></span>
                </td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td valign="top" class="aux_intable_td1">接单人：</td>
            <td class="aux_intable_td2">
            <table border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td>
                	<input id="deptPrincipalName"
                			name="deptPrincipalName"
                			type="text"
                			class="inputtext  max-length-100"
                			afterInsert="personErrorMessage"
                			readonly="readonly"
                			value=""
                			size="16" />
                </td>
                <td width="4">&nbsp;</td>
                <td>
					<a href="#"  onClick="openSelectPerson();" onFocus="this.blur()">
						<img src="${ctx}/images/ico_cz.gif" width="16" height="16" border="0" />
					</a>
					<span id="personErrorMessage"></span>
                </td>
              </tr>
            </table></td>
            </tr>
        </table></td>
      </tr>
    </table>

    </td>
    </form>
  </tr>
  <tr>

    <td class="window_footer">
    <table border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>&nbsp;</td>
        <td class="btn_between_space">&nbsp;</td>
        <td><table border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td class="buttonOffLeft" id="btn1_11"></td>
            <td class="buttonOffMiddle" id="btn1_12"
            	onClick="submitForm();"
            	onMouseOver="btn_change_over(1)"
            	onMouseOut="btn_change_out(1)"
            	onBlur="btn_change(1)">
            	<img src="${ctx}/images/ico_ok.gif" alt="" width="14"
            		height="14" border="0" align="absbottom" /> 提 交</td>
            <td class="buttonOffRight" id="btn1_13"></td>
          </tr>
        </table></td>
        <td class="btn_between_space"></td>
        <td><table border="0" cellpadding="0" cellspacing="0">
            <tr>
              <td class="buttonOffLeft" id="btn1_31"></td>
              <td class="buttonOffMiddle" id="btn1_32"
              		onClick="windowClose();"
              		onMouseOver="btn_change_over(3)"
              		onMouseOut="btn_change_out(3)"
              		onBlur="btn_change(3)">
              		<img src="${ctx}/images/ico_cancel.gif" alt="" width="14"
              			height="14" border="0" align="absbottom" /> 关 闭</td>
              <td class="buttonOffRight" id="btn1_33"></td>
            </tr>
        </table></td>
      </tr>
    </table>
    <p>&nbsp;</p></td>
  </tr>
</table>
</body>
</html>